<?php

namespace Addons\CheckIn\Controller;

use Home\Controller\AddonsController;
use Think\Hook;

class CheckInController extends AddonsController
{

    public function doCheckIn()
    {
        if (!is_login()) {
            $this->error('请先登陆！');
        }
        $time = get_some_day(0);
        $uid = is_login();
        $model = D('Addons://CheckIn/CheckIn');
        $memberModel = D('Member');
        $check = $model->getCheck($uid);
        if (!$check) {
            $model->addCheck($uid);
            $memberModel->where(array('uid' => $uid))->setInc('total_check');
            $model->checkYesterday($uid);

            S('check_rank_today_' . $time, null);
            S('check_rank_con_' . $time, null);
            S('check_rank_total_' . $time, null);
            $this->success('签到成功');
        } else {
            $this->error('已经签到了！');
        }
    }



    public function getRank(){
        $aType = I('post.type','today','op_t');
        $name= get_addon_class('CheckIn');
        $class = new $name();
        $html = $class->rank($aType);
        $this->ajaxReturn(array('status'=>1,'html'=>$html));
    }


    public function ranking()
    {
        $aPage = I('get.page',1,'intval');
        $aOrder = I('get.order','con_check','op_t');
        $checkInfoModel = D('Addons://CheckIn/CheckIn');
        $memberModel=D('Member');
        $time = get_some_day(0);
        $limit = 50 ;
        $user_list=$memberModel->field('uid,nickname,total_check,con_check')->page($aPage,$limit)->order($aOrder.' desc,uid asc')->select();
        $totalCount = $memberModel->count();

        $time = get_some_day(0);
        foreach ($user_list as $key => &$val) {
            $val['ranking'] = ($aPage - 1) * $limit + $key + 1;


            if ($val['ranking'] <= 3) {
                $val['ranking'] = '<span style="color:#EB7112;">' . $val['ranking'] . '</span>';
            }
            $check =$checkInfoModel->getCheck($val['uid']);
            if ($check) {
                $val['status'] = '<span>已签到</span>';
            } else {
                $val['status'] = '<span style="color: #BDBDBD;">未签到</span>';
            }

        }

        $this->assign('user_list', $user_list);
        $this->assign('totalCount', $totalCount);

        $this->display(T('Addons://CheckIn@CheckIn/ranking'));




        if(is_login()){
            //获取用户信息
            $user_info = query_user(array('uid', 'nickname', 'space_url', 'avatar64',), is_login());
            $check_info=$checkInfoModel->where(array('uid'=>is_login()))->find();

            if(!$check_info){
                $check_info['con_num']=0;
                $check_info['total_num']=0;
                $check_info['total_score']=0;
                $check_info['is_sign']=0;
            }else{
                if($check_info['ctime']>=get_some_day(0)){
                    $check_info['is_sign']=1;
                }else{
                    $check_info['is_sign']=0;
                }
            }
            $user_info=array_merge($user_info,$check_info);
            $ranking = $checkInfoModel->field('uid')->order('total_num desc,uid asc')->select();
            $ranking = array_column($ranking, 'uid');
            if(array_search(is_login(), $ranking)===false){
                $user_info['ranking'] = count($ranking) + 1;
            }else{
                $user_info['ranking'] = array_search(is_login(), $ranking) + 1;
            }
            $this->assign('user_info', $user_info);
            //获取用户信息end
        }


    }




}